#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Date    : 2020-08-27 15:05:42
# @Author  : Zuo Zhongfei (zuozhongfei@126.com)
# @Link    : www
# @Version : $Id$

import os


def count_words(filename):
    """计算一个文件大致包含多少个单词"""
    try:
        with open(filename) as file_object:
            contents = file_object.read()
    except FileNotFoundError as e:
        msg = "Sorry, the file " + filename + " does not exist"
        print(msg)
    else:
        # 计算文件包含多少个单词
        words = contents.split()
        num_words = len(words)
        print("The file " + filename +
              ' has about ' + str(num_words) + ' words')


# 编写一个文件列表，然后for循环这个列表，调用计算单词函数
filenames = ['../../file/chapter_10/alice.txt',
             '../../file/chapter_10/siddhartha.txt',
             '../../file/chapter_10/moby_dict.txt',
             '../../file/chapter_10/little_women.txt',
             '../../file/chapter_10/not_exist_file.txt']
for filename in filenames:
    count_words(filename)
